﻿<%@ Control Language="C#" AutoEventWireup="true" CodeFile="tm_admin_Tree.ascx.cs" Inherits="tm_TreeTab" %>
<script type="text/javascript">
function OnTreeClick(evt)
   {
        var src = window.event != window.undefined ? window.event.srcElement : evt.target;
        var isChkBoxClick = (src.tagName.toLowerCase() == "input" && src.type == "checkbox");
        if(isChkBoxClick)
        {
            var parentTable = GetParentByTagName("table", src);
            var nxtSibling = parentTable.nextSibling;
            if(nxtSibling && nxtSibling.nodeType == 1)//check if nxt sibling is not null & is an element node
            {
                if(nxtSibling.tagName.toLowerCase() == "div") //if node has children
                {
                    //check or uncheck children at all levels
                    CheckUncheckChildren(parentTable.nextSibling, src.checked);
                }
            }
            //check or uncheck parents at all levels
            CheckUncheckParents(src, src.checked);
        }
   }
   function CheckUncheckChildren(childContainer, check)
   {
      var childChkBoxes = childContainer.getElementsByTagName("input");
      var childChkBoxCount = childChkBoxes.length;
      for(var i = 0; i<childChkBoxCount; i++)
      {
        childChkBoxes[i].checked = check;
      }
   }
   function CheckUncheckParents(srcChild, check)
   {
       var parentDiv = GetParentByTagName("div", srcChild);
       var parentNodeTable = parentDiv.previousSibling;
      
       if(parentNodeTable)
        {
            var checkUncheckSwitch;
           
            if(check) //checkbox checked
            {
                var isAllSiblingsChecked = AreAllSiblingsChecked(srcChild);
                if(isAllSiblingsChecked)
                    checkUncheckSwitch = true;
                else   
                    return; //do not need to check parent if any(one or more) child not checked
            }
            else //checkbox unchecked
            {
                checkUncheckSwitch = false;
            }
           
            var inpElemsInParentTable = parentNodeTable.getElementsByTagName("input");
            if(inpElemsInParentTable.length > 0)
            {
                var parentNodeChkBox = inpElemsInParentTable[0];
                parentNodeChkBox.checked = checkUncheckSwitch;
                //do the same recursively
                CheckUncheckParents(parentNodeChkBox, checkUncheckSwitch);
            }
        }
   }
   function AreAllSiblingsChecked(chkBox)
   {
     var parentDiv = GetParentByTagName("div", chkBox);
     var childCount = parentDiv.childNodes.length;
     for(var i=0; i<childCount; i++)
     {
        if(parentDiv.childNodes[i].nodeType == 1) //check if the child node is an element node
        {
            if(parentDiv.childNodes[i].tagName.toLowerCase() == "table")
            {
               var prevChkBox = parentDiv.childNodes[i].getElementsByTagName("input")[0];
              //if any of sibling nodes are not checked, return false
              if(!prevChkBox.checked)
              {
                return false;
              }
            }
        }
     }
     return true;
   }
   //utility function to get the container of an element by tagname
   function GetParentByTagName(parentTagName, childElementObj)
   {
      var parent = childElementObj.parentNode;
      while(parent.tagName.toLowerCase() != parentTagName.toLowerCase())
      {
         parent = parent.parentNode;
      }
    return parent;   
   }

</script>

<table border="0" cellpadding="1" cellspacing="1" width="100%">
    <tr>
        <td colspan="2">
            <asp:Panel ID="Panel1" runat="server" Width="100%">
                <table border="0" style="width: 100%">
        <tr>
            <td style="width: 12%">
                <asp:Label ID="Label4" runat="server" Text="Bảng mã :" CssClass="titletext3"></asp:Label></td>
            <td>
                <asp:DropDownList ID="ddlTable" runat="server">
                    <asp:ListItem Value="tm_productCat">Ph&#226;n loại sản phẩm</asp:ListItem>
                    <asp:ListItem Value="tm_spccode">L&#227;nh vực &#225;p dụng</asp:ListItem>
                    <asp:ListItem Value="tm_activityCat">L&#227;nh vực hoạt động</asp:ListItem>
                </asp:DropDownList>
                <asp:CheckBox ID="chkShowTree" runat="server" Text="Xem cấu trúc " CssClass="titletext3" /></td>
        </tr>
        <tr>
            <td style="width: 12%">
                <asp:Label ID="Label5" runat="server" Text="Mô tả :" CssClass="titletext3"></asp:Label></td>
            <td>
                <asp:TextBox ID="txtDescription" runat="server" Width="281px"></asp:TextBox>
                <asp:CheckBox ID="chkSearchEachWord" runat="server" Text="Tìm từng từ" CssClass="titletext3" /></td>
        </tr>
        <tr>
            <td style="width: 12%;">
                <asp:Label ID="Label6" runat="server" Text="Mã số :" CssClass="titletext3"></asp:Label></td>
            <td >
                <asp:TextBox ID="txtSysCode" runat="server" Width="137px"></asp:TextBox>
            </td>
        </tr>
                    <tr>
                        <td >
                            <asp:Label ID="Label1" runat="server" CssClass="titletext3" Text="Mã gốc :"></asp:Label></td>
                        <td >
                            <asp:TextBox ID="txtParentCode" runat="server" Width="137px"></asp:TextBox></td>
                    </tr>
        <tr>
            <td style="width: 12%;">
            </td>
            <td >
                <asp:Label ID="lblMessage" runat="server"></asp:Label></td>
        </tr>
        <tr>
            <td style="width: 12%">
            </td>
            <td>
                <asp:Button ID="btnFilter" runat="server" CssClass="btn" OnClick="btnFilter_Click"
                    Text="Đồng ý" Width="80px" />&nbsp;</td>
        </tr>
    </table>
            </asp:Panel>
            
            <asp:Panel ID="paUpdate" runat="server" Width="100%" Visible="False">
            <table border="0" width="100%" class="table" cellspacing="1">
                <hr />
                <tr>
                    <td style="width: 12%;">
                    
                    </td>
                    <td style="width: 78%;">
                             <asp:Label ID="lblEditTitle" runat="server" CssClass="titletext2" EnableViewState="true"></asp:Label></td>
                </tr>
                <tr>
                    <td style="width: 12%">
            <asp:Label ID="Label3" runat="server" CssClass="titletext3" Text="Mã số :"></asp:Label><span
                            style="color: #ff0000">*</span></td>
                    <td style="width: 78%">
                        <asp:TextBox ID="txtUpdateSysCode" runat="server" Width="81px"></asp:TextBox>
                        &nbsp;
                        <asp:CheckBox ID="chkAddNew" runat="server" Text="Thêm mới" CssClass="titletext3" />
                        <asp:Label ID="lblCurSyscode" runat="server" Visible="False"></asp:Label></td>
                </tr>
	<tr>
		<td style="width: 12%">
            <asp:Label ID="Label2" runat="server" CssClass="titletext3" Text="Mô tả :"></asp:Label>
            <span style="color: red">*</span></td>
		<td style="width: 78%"><asp:TextBox ID="txtUpdateDescription" runat="server" Width="60%" TextMode="MultiLine" Height="60px"></asp:TextBox></td>
	</tr>
	<tr>
		<td style="width: 12%">
            
                        <asp:Label ID="Label7" runat="server" CssClass="titletext3" Text="Mã cấp trên :"></asp:Label></td>
		<td style="width: 78%">
                        <asp:TextBox ID="txtUpdateParentCode" runat="server" Width="81px"></asp:TextBox></td>
	</tr>
                <tr>
                    <td style="width: 12%">
                    </td>
                    <td style="width: 78%">
                        <asp:Label ID="lblMessages" runat="server"></asp:Label></td>
                </tr>
                <tr>
                    <td style="width: 12%">
                    </td>
                    <td style="width: 78%">
                        <asp:Button ID="btnSave" runat="server" CssClass="btn" OnClick="btnSave_Click"
                        Text="Lưu" Width="85px" /><asp:Button ID="btnDelete" runat="server" CssClass="btn" OnClick="btnDelete_Click"
                        OnClientClick="return confirm('Xóa dữ liệu này ?')" Text="Xóa" Width="85px" /></td>
                </tr>
</table>
            </asp:Panel>
        </td>
    </tr>
</table>

<table border="0" cellpadding="1" cellspacing="1" width="100%">
    <tr>
        <td colspan="2" valign="top">
            <asp:Label ID="lblResultCount" runat="server" CssClass="titleText3" Visible="False"></asp:Label>
           <asp:GridView ID="gvCodeCat" runat="server" AutoGenerateColumns="False" Width="100%" AllowPaging="True" CssClass="table" OnPageIndexChanging="gvCodeCat_PageIndexChanging">
        <Columns>
            <asp:BoundField DataField="code" HeaderText="M&#227;" >
                <ItemStyle Width="10%" />
            </asp:BoundField>
            <asp:TemplateField HeaderText="M&#244; tả">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("description") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# GetURL(Eval("code").ToString(),Eval("description").ToString()) %>'
                        Text='<%# Bind("description") %>'></asp:HyperLink>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="parentCode" HeaderText="M&#227;.gốc">
                <ItemStyle Width="10%" />
            </asp:BoundField>
            <asp:TemplateField>
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </EditItemTemplate>
                <ItemStyle Width="5%" />
                <ItemTemplate>
                    <asp:HyperLink ID="HyperLink1" runat="server" ImageUrl="~/images/myEdit.gif" NavigateUrl='<%# GetURL(Eval("code").ToString(),Eval("description").ToString()) %>'
                        Text='<%# Bind("description") %>'></asp:HyperLink>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
               <PagerStyle HorizontalAlign="Right" />
    </asp:GridView> 
        </td>
    </tr>
</table>

<asp:Panel ID="paTreeview" runat="server" BorderColor="#8080FF" BorderStyle="Solid"
    BorderWidth="1px" ScrollBars="Auto" Height="300px" Visible="False">
<asp:TreeView ID="TreeViewObj" runat="server" OnAdaptedSelectedNodeChanged="OnTreeSelection"
            OnTreeNodePopulate="TreeViewObj_TreeNodePopulate" Width="194px" OnSelectedNodeChanged="TreeViewObj_SelectedNodeChanged">
        </asp:TreeView>
    </asp:Panel>
